package com.cpi.cornpp.transports.action;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.corntypes.view.CornTypesView;
import com.cpi.cornpp.transports.exception.TransportException;
import com.cpi.cornpp.transports.form.TransportForm;
import com.cpi.cornpp.transports.service.TransportService;
import com.cpi.cornpp.transports.view.TransportView;

public class SaveAddNewTransportAction extends Action {
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	private final static String ERROR_MESSAGE = "ERROR_MESSAGE";//$NON-NLS-1$
	private static final String FAILURE_FORWARD = "error";
	private static final String TRANSPORT_LIST_FORWARD = "transportsList";
	private static final String ADD_NEW_TRANSPORT_FORWARD = "addNewTransport";
	private static final String UNEXPECTED_ERROR_MESSAGE_KEY = "error.message.common.unexpectedError";
	/** Information message name*/
	private final static String MESSAGE = "MESSAGE";//$NON-NLS-1$
	/** Success add key */
	private static final String SUCCESS_ADD_LABEL = "common.add.success";//$NON-NLS-1$
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws TransportException{
		String messageError = null;
		String addNewTransport = null;
		TransportView transportView = null;
		CornTypesView cornTypeView = null;
		TransportForm transportForm = null;
		TransportService transportService = null;
		HttpSession session = null;
		ActionForward forward = null;
		MessageResources messageResources = null;
		
		forward = mapping.findForward(FAILURE_FORWARD);
		
		try{
			
			session = request.getSession(false);
			messageResources = getResources(request);
			transportView = (TransportView) session.getAttribute("transportView");
			cornTypeView = new CornTypesView();
			
			if ((form != null) && (transportView != null)){
				transportForm = (TransportForm) form;
				
				transportService = new TransportService();
				addNewTransport = transportForm.getCreateNewTransport();
				
				transportView.setTransportShortName(transportForm.getCode());
				transportView.setTransportDescription(transportForm.getDescription());
				transportView.setTransportCapacity((StringUtils.isNotEmpty(transportForm.getCapacity())) ? Double.parseDouble(transportForm.getCapacity()) : 0.0D);
				transportView.setTransportPriority((StringUtils.isNotEmpty(transportForm.getPriority())) ? Integer.parseInt(transportForm.getPriority()) : 0);
				transportView.setTransportNomenclature(transportForm.getNomenclature());
				cornTypeView.setCornTypeId(transportForm.getCornTypeId());
				transportView.setCornTypesView(cornTypeView);
				
				transportService.addTransport(transportView);
				
				messageError = messageResources.getMessage((Locale)session.getAttribute(Globals.LOCALE_KEY), SUCCESS_ADD_LABEL);
				session.setAttribute(MESSAGE, messageError);
				
				forward = ("1".equalsIgnoreCase(addNewTransport) ? mapping.findForward(ADD_NEW_TRANSPORT_FORWARD) : mapping.findForward(TRANSPORT_LIST_FORWARD));
			} else {
				TransportException tre = new TransportException(UNEXPECTED_ERROR_MESSAGE_KEY, TransportException.LAYER_ACTION, TransportException.ACTION_INSERT);
				
				throw tre;
			}
			
		} catch(TransportException tre){
			messageError = messageResources.getMessage((Locale)session.getAttribute(Globals.LOCALE_KEY), tre.getErrorCode());
			
			if(null == messageError){
				session.setAttribute(CUSTOM_EXCEPTION, tre);
				form.reset(mapping, request);
				throw tre;
			}else{
				session.setAttribute(ERROR_MESSAGE, messageError);
				forward = mapping.findForward(FAILURE_FORWARD);
			}
			
		} catch(Exception e){
			TransportException tre = new TransportException(e , TransportException.LAYER_ACTION, TransportException.ACTION_INSERT);;
			
			session.setAttribute(CUSTOM_EXCEPTION, tre);
			form.reset(mapping, request);
			e.printStackTrace(System.out);
			
			throw tre;
		} finally {
			session.removeAttribute("transportView");
			session.removeAttribute("deletedCostElementTransport");
			session.removeAttribute("addedCostElementsTransport");
		}
		
		return forward;
	}
	
}
